package com.e1xp.deport_management_system.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.e1xp.deport_management_system.entity.Product;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

/**
 * <p>
 * Mapper 接口
 * </p>
 *
 * @author E1xP
 * @since 2021-04-09
 */
public interface ProductMapper extends BaseMapper<Product> {

    @Select("SELECt IFNULL(sum(count),0) FROM product WHERE instance_id=#{instanceId}")
    long getSumProduct(long instanceId);

    @Select("<script> " +
            "SELECT * FROM product WHERE instance_id=#{instanceId} AND" +
            "<foreach item='item' collection='productIdList' index='index' separator=' OR ' open='(' close=')'>" +
            "id=#{item}" +
            "</foreach> " +
            "</script>")
    List<Product> getFromIdList(@Param("instanceId") long instanceId, @Param("productIdList") List<Long> productIdList);

    @Update("<script>" +
            "UPDATE product set unsell= " +
            "case " +
            "<foreach item='item' collection='productList' index='index'>" +
            "when id=#{item.id} then #{item.unsell} " +
            "</foreach>" +
            "END " +
            "WHERE id in" +
            "<foreach item='item' collection='productList' index='index' separator=',' open='(' close=')'>" +
            "#{item.id}" +
            "</foreach>" +
            "</script>")
    void updateList(@Param("productList") List<Product> productList);

    @Select("SELECT product_id from order_product where instance_id=#{instanceId} and order_id=#{orderId}")
    List<Long> getProductIdFromOrderId(@Param("instanceId") long instanceId, @Param("orderId") long orderId);
}
